/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * addProduccion.java
 *
 * Created on Apr 30, 2010, 8:16:43 PM
 */

package UI;

import Excepciones.BasesdeDatos;
import Excepciones.DinamicaDato;
import Excepciones.InventarioException;
import Excepciones.ProcesamientoDato;
import Excepciones.Sistema;
import com.mysql.jdbc.exceptions.jdbc4.CommunicationsException;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JTable;
import Modelo.BaseDato;
import robot_maker.Inventario;
import robot_maker.ResultSetTableModel;

/**
 *
 * @author CarlosScipione
 */
public class addProduccion extends javax.swing.JFrame {
    private int fila;
    private ResultSet lista;
    private int rowMyor;
    private UI_Produccion owner;
    /** Creates new form addProduccion */
    public addProduccion(UI_Produccion dueno) {
        initComponents();
        owner = dueno;
        this.setDefaultCloseOperation(HIDE_ON_CLOSE);
        this.combito.add(acc_bcheck);
        this.combito.add(robot_bcheck);
    }

    private addProduccion() {

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */

    private void llenar (JTable tabla, String query){
            try{
            try {
                BaseDato buscar = new BaseDato();
            } catch (CommunicationsException ex) {
                        Sistema.loggear(10, "LLENAR",
                        "COMUNICACION PERDIDA CON LA BASE DE DATOS",
                        addProduccion.class, "CommunicationsException");
            } catch (DinamicaDato ex) {
            }
            tabla.setModel(new ResultSetTableModel(query));

            if (tabla.getRowCount() > 0) {
                tabla.setRowSelectionInterval(0, 0);
            }


        } catch (ClassNotFoundException ex) {
        Sistema.loggear(21, "llenar", "SQL SYNTAXIS", Inventario.class,
                    "ClassNotFoundException");
        } catch (SQLException ex) {
        Sistema.loggear(20, "llenar", "SQL SYNTAXIS", Inventario.class,
                    "SQLException");
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        combito = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        cantidad_label = new javax.swing.JLabel();
        cantidad_text = new javax.swing.JFormattedTextField();
        robot_bcheck = new javax.swing.JToggleButton();
        acc_bcheck = new javax.swing.JToggleButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        comun_table = new javax.swing.JTable();
        producir_boton = new javax.swing.JButton();
        cancel_boton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setText("Produccion:");

        cantidad_label.setText("Cantidad:");

        cantidad_text.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));

        robot_bcheck.setText("Robots");
        robot_bcheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                robot_bcheckActionPerformed(evt);
            }
        });

        acc_bcheck.setText("Accesorios");
        acc_bcheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                acc_bcheckActionPerformed(evt);
            }
        });

        comun_table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        comun_table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                comun_tableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(comun_table);

        producir_boton.setText("Producir");
        producir_boton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                producir_botonActionPerformed(evt);
            }
        });

        cancel_boton.setText("Cancelar");
        cancel_boton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancel_botonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(layout.createSequentialGroup()
                        .add(cancel_boton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 81, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(producir_boton))
                    .add(jLabel1)
                    .add(acc_bcheck, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(cantidad_label)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cantidad_text, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 113, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(robot_bcheck, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .add(18, 18, 18)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jLabel1)
                        .add(18, 18, 18)
                        .add(robot_bcheck, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 44, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(acc_bcheck, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 42, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(21, 21, 21)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(cantidad_label)
                            .add(cantidad_text, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(layout.createSequentialGroup()
                        .add(54, 54, 54)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 217, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                .add(producir_boton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 45, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(cancel_boton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 46, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void robot_bcheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_robot_bcheckActionPerformed
    this.comun_table.setEnabled(true);
    llenar(comun_table,
      "select id \"ID\", nombre \"Nombre\", peso \"Peso\", tamano \"Tamaño\", "+
      "descripcion \"Descripcion\" from ROBOT");
    }//GEN-LAST:event_robot_bcheckActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        this.comun_table.setEnabled(false);
        this.producir_boton.setEnabled(false);

    }//GEN-LAST:event_formWindowOpened

    private void acc_bcheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_acc_bcheckActionPerformed
    this.comun_table.setEnabled(true);
    llenar(comun_table, "select id \"ID\", nombre \"Nombre\", " +
            "CARACTERISTICAS \"Caracteristicas\" from ACCESORIO");
    }//GEN-LAST:event_acc_bcheckActionPerformed

    private void cancel_botonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancel_botonActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_cancel_botonActionPerformed

    private void producir_botonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_producir_botonActionPerformed

      
        if(robot_bcheck.isSelected()){
                try {
                    lista = BaseDato.ejecutarSqlSelect("select id \"ID\", " 
                      + "nombre \"Nombre\", peso \"Peso\", tamano \"Tamaño\", "
                      + "descripcion \"Descripcion\" from ROBOT");
                } catch (DinamicaDato ex) {
                }

        }
        else{
                try {
                    lista = BaseDato.ejecutarSqlSelect("select id \"ID\", " + 
                      "nombre \"Nombre\", CARACTERISTICAS \"Caracteristicas\" "
                      + "from ACCESORIO");
                } catch (DinamicaDato ex) {
                }
        }

        int i = 0;

        while (i <= rowMyor){
            try {
                lista.next();
                i++;
            } catch (SQLException ex) {
         Sistema.loggear(19, "producir", "SQL SYNTAXIS", Inventario.class,
                    "SQLException");
            }

        }
        if(robot_bcheck.isSelected()){
            try {
                Inventario produccion;
                try {
                    produccion = new Inventario(
                            Integer.parseInt(lista.getString(1)),
                            Integer.parseInt(lista.getString(1)),
                            this.cantidad_text.getText());

                    
                        produccion.producir(produccion, true);
                        } catch (InventarioException ex) {
                } catch (ProcesamientoDato ex) {
                    }
                this.setVisible(false);
                owner.refrescar();

            } catch (SQLException ex) {
    Sistema.loggear(19, "producir", "SQL SYNTAXIS", Inventario.class,
                    "SQLException");
            }
        }
        else{
            try {
                Inventario produccion;
                try {
                    produccion = new Inventario(
                            Integer.parseInt(lista.getString(1)),
                            Integer.parseInt(lista.getString(1)),
                            this.cantidad_text.getText());

                    
                        produccion.producir(produccion, false);
                        } catch (InventarioException ex) {
                }
                     catch (ProcesamientoDato ex) {
                    }
                this.setVisible(false);
                owner.refrescar();

            } catch (SQLException ex) {
    Sistema.loggear(19, "producir", "SQL SYNTAXIS", Inventario.class,
                    "SQLException");
            }
        }

        
    }//GEN-LAST:event_producir_botonActionPerformed

    private void comun_tableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_comun_tableMouseClicked
        JTable target = (JTable)evt.getSource();
        rowMyor = target.getSelectedRow();
        int column = target.getSelectedColumn();
        this.producir_boton.setEnabled(true);



    }//GEN-LAST:event_comun_tableMouseClicked

  private void jTable1SelectionChanged(javax.swing.event.ListSelectionEvent evt) {


        if (this.comun_table.getRowCount()>0 && 
                !this.comun_table.getSelectionModel().getValueIsAdjusting() &&
                this.comun_table.getSelectedRow()>-1){

            this.fila = comun_table.getSelectedRow();



                String id =comun_table.getValueAt(this.fila, 0).toString();



        }
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new addProduccion().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton acc_bcheck;
    private javax.swing.JButton cancel_boton;
    private javax.swing.JLabel cantidad_label;
    private javax.swing.JFormattedTextField cantidad_text;
    private javax.swing.ButtonGroup combito;
    private javax.swing.JTable comun_table;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton producir_boton;
    private javax.swing.JToggleButton robot_bcheck;
    // End of variables declaration//GEN-END:variables

}
